package kd.comt.plugin;

import java.util.ArrayList;
import java.util.EventObject;
import java.util.List;

import kd.bos.entity.tree.TreeNode;
import kd.bos.form.control.TreeView;
import kd.bos.form.control.events.TreeNodeCheckEvent;
import kd.bos.form.control.events.TreeNodeCheckListener;
import kd.bos.form.control.events.TreeNodeClickListener;
import kd.bos.form.control.events.TreeNodeDragEvent;
import kd.bos.form.control.events.TreeNodeDragListener;
import kd.bos.form.control.events.TreeNodeEvent;
import kd.bos.form.control.events.TreeNodeQueryListener;
import kd.bos.form.plugin.AbstractFormPlugin;

public class TreeCS extends AbstractFormPlugin implements TreeNodeClickListener, TreeNodeCheckListener, TreeNodeQueryListener,TreeNodeDragListener{
    
    private final static String KEY_TREEVIEW1 = "bpgx_treeviewap";//树控件
    
    /**
     * 注册控件事件
     */
    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);
 
        TreeView treeView = this.getView().getControl(KEY_TREEVIEW1);
        // 侦听树节点懒加载事件
        treeView.addTreeNodeQueryListener(this);
        // 侦听树节点点击事件，Click
        treeView.addTreeNodeClickListener(this);
        // 侦听树节点勾选事件，Check:特别注意字母拼接差异Check、Click
        treeView.addTreeNodeCheckListener(this);
        // 侦听树节点拖拽移动事件
        treeView.addTreeNodeDragListener(this);
    }

    

    //拖拽节点
    @Override
    public void treeNodeDragged(TreeNodeDragEvent arg0) {
        // TODO Auto-generated method stub
        
    }
//查询子节点
    @Override
    public void queryTreeNodeChildren(TreeNodeEvent arg0) {
        // TODO Auto-generated method stub
        String nodeId = (String)arg0.getNodeId();
 
        List<TreeNode> childNodes = this.loadChildNode(nodeId);
 
        TreeView treeView = this.getView().getControl(KEY_TREEVIEW1);
        treeView.addNodes(childNodes);
    }
//树节点检查
    @Override
    public void treeNodeCheck(TreeNodeCheckEvent arg0) {
        // TODO Auto-generated method stub
        
    }
    
    /**
     * 加载子节点
     * @param parentId
     * @return
     */
    private List<TreeNode> loadChildNode(String parentId){
 
        List<TreeNode> childNodes = new ArrayList<>();
 
        // 演示代码，直接创建新的节点返回，略过读取数据库的环节
        String id1 = parentId + ".1";
        TreeNode node1 = new TreeNode(parentId, id1, id1);
        node1.setChildren(new ArrayList<>());        // 需要懒加载下级节点
 
        childNodes.add(node1);
 
        String id2 = parentId + ".2";
        TreeNode node2 = new TreeNode(parentId, id2, id2);
        node2.setChildren(new ArrayList<>());        // 需要懒加载下级节点
 
        childNodes.add(node2);
 
        return childNodes;
    }
 
}

